<?php

namespace common\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\MktChat;
use yii\db\Query;

/**
 * MktChatSearch represents the model behind the search form about `common\models\MktChat`.
 */
class MktChatSearch extends MktChat
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'sender_id', 'recver_id', 'is_read', 'is_deleted','cat', 'approved'], 'integer'],
            [['content', 'create_time', 'update_time'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = MktChat::find()->orderBy(['id' => SORT_DESC]);;

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        if (!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
            'sender_id' => $this->sender_id,
            'recver_id' => $this->recver_id,
            'is_read' => $this->is_read,
            'is_deleted' => $this->is_deleted,
            'create_time' => $this->create_time,
            'update_time' => $this->update_time,
            'cat' => $this->cat,
            'approved' => $this->approved,
        ]);

        $query->andFilterWhere(['like', 'content', $this->content]);

        return $dataProvider;
    }

    public function searchWap($params)
    {
        $query = MktChat::find()->orderBy(['id' => SORT_DESC])->limit(50);

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        if (!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
            'sender_id' => $this->sender_id,
            'recver_id' => $this->recver_id,
            'is_read' => $this->is_read,
            'is_deleted' => $this->is_deleted,
            'cat' => $this->cat,
            'approved' => $this->approved,
            'create_time' => $this->create_time,
            'update_time' => $this->update_time,
        ]);

        $query->andFilterWhere(['like', 'content', $this->content]);

        return $dataProvider;
    }

    public function searchWapGroup($params)
    {
        //SELECT * from (SELECT * from mkt_chat order by id desc) aaa group by sender_id order by id desc
        $query = MktChat::find();
        $subQuery = (new Query())->select('*')->from('mkt_chat')->where(['is_deleted' => 0])->orderBy(['id' => SORT_DESC]);
        $query->from(['aaa' => $subQuery])->groupBy(['sender_id'])->orderBy(['id' => SORT_DESC])->limit(50);

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        if (!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
            'sender_id' => $this->sender_id,
            'recver_id' => $this->recver_id,
            'is_read' => $this->is_read,
            'is_deleted' => $this->is_deleted,
            'cat' => $this->cat,
            'approved' => $this->approved,
            'create_time' => $this->create_time,
            'update_time' => $this->update_time,
        ]);

        $query->andFilterWhere(['like', 'content', $this->content]);

        return $dataProvider;
    }

}
